from aip import AipOcr
import os
from pathlib import Path
from timeDeco import timeDeco

desk_path = r'C:\Users\***\Desktop'
os.chdir(r'C:\Users\***\Desktop')


def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()


def tedWrite(lsts):
    APP_ID = '***'
    API_KEY = '***'
    SECRET_KEY = '***'
    client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
    with open('IELTS_01.txt', 'a+') as txt:
        for lst in lsts:
            image = get_file_content(lst)
            text = client.basicAccurate(image)
            result = text["words_result"]
            for i in result:
                txt.write(i["words"])
                txt.write('\n')


@timeDeco
def liu():
    # pool = multiprocessing.Pool(processes=6)
    p = Path(desk_path)
    lsts = list(p.glob('*Screen*.png'))
    # res = [pool.apply_async(tedWrite, args=(lst,)) for lst in lsts]
    # pool.close()
    # pool.join()
    tedWrite(lsts)
    lines_seen = set()
    outfile = 'IELTS_01_noD.txt'
    with open(outfile, 'a+') as outf:
        with open('IELTS_01.txt', 'r') as f:
            for line in f:
                line = line.strip('\n')
                if line not in lines_seen:
                    outf.write(line + '\n')
                    lines_seen.add(line)


if __name__ == "__main__":
    liu()
